<!--榜单-->
<template>
  <div class="all bg1" @viewappear="viewappear">

    <!--菜单按钮-->
    <div class="row jcenter">
      <VSubMenuOval class="menu" :menus="menus" :menu="menu" @menuClick="menuClick"></VSubMenuOval>
    </div>


    <!-- 列表 -->
    <list class="list">
    
      <!--刷新-->
      <refresh :display="isRefresh ? 'show' : 'hide'" @refresh="httpRanking()">
        <loading-indicator class="load text1"></loading-indicator>
      </refresh>

      <!--内容-->
      <cell v-for="(item, index) in datas" v-bind:key="index">
        
        <VRanking class="cell" :item="item" :index="index"
        @click="infoClick" @follow="followClick" @copy="copyClick" />

        <!-- 无更多数据 -->
        <div class="genduo" v-if="(index == datas.length - 1)">
          <text class="font22 text3">{{T('没有更多数据')}}</text>
        </div>
      </cell>
    </list>
  </div>
</template>

<style src="@/_css/style.css" ></style>
<style scoped>
.menu {
  margin-top: 20px;
}
.load {
  height: 40px;
  width: 40px;

  margin-top: 20px;
  margin-left: 355px;
}
.genduo {
  justify-content: center;
  align-items: center;
  height: 100px;
}

/*********************** 列表*/
.list {
  flex: 1;
  margin-top: 15px;
}
.cell {
  margin-left: 16px;
  margin-right: 16px;
  margin-top: 15px;
  margin-bottom: 15px;
}
</style>

<script>

//组件
import VSubMenuOval from '@/_public/VSubMenuOval.vue' 
import VRanking from '@/square/view/VRanking.vue' 

//功能
import jnavigator from "@/_js/navigator";
import jconfig from "@/_js/config";
import jmodal from "@/_js/modal";
import jmath from "@/_js/math";
import jhttps from "@/_js/https";

const weexFunction = weex.requireModule("WeexFunction");

import I18N from "@/_i18n/i18n";
var T = I18N.t;

export default {

  components: {
    VSubMenuOval,
    VRanking,
  },

  data() {
    return {
      T:T,
      onShow:false,

      menu: 1, //菜单
      menus: [T('币币榜'), T('合约榜')], //菜单
      menuKeys:['spot', 'contract_usdt'], //菜单对应关键字

      isRefresh:false, //显示刷新
      datas:[],
    };
  },

  created() {
    // setTimeout(() => {
    //   this.httpRanking(true);
    // }, 500);
  },

  mounted() {
    setTimeout(() => {
      this.httpRanking(true);
    }, 500);
  },

  methods: {

    initData(datas) {

      //临时隐藏投入资金为负数的排行数据
      var list = [];
      for (let index = 0; index < datas.length; index++) {
        var map = datas[index];

        //临时处理，小于100投入，默认显示100 - 2021.10.14取消临时处理
        // map.investCoinnNum = Number(map.investCoinnNum) < 100 ? '100' : map.investCoinnNum;
        // map.investCoinnNum = jmath.gfCoin(map.investCoinnNum, map.investCoin, false);

        map.realEarnings = jmath.gfCoin(map.realEarnings, map.investCoin, false);
        if (map.investCoinnNum > 0) {
          list.push(map);
        }
      }

      if (this.menu == '币币榜') {
        return list;
      }

      //初始化合约策略策略方向，倍数
      for (let index = 0; index < list.length; index++) {

        var map = list[index];
        if (map.config == undefined) {
          map.contract_num = 1;
          map.contract_show = T('合约');
          continue;
        }

        var pair = map.config.tradePairs[0];
        var investType = pair.investType + '';
        switch (investType) {
          case '0': 
            map.contract_num = 1;
            map.contract_show = T('做多') + pair.leverRate + 'X';
            break;
          case '1':
            map.contract_num = -1;
            map.contract_show = T('做空') + pair.leverRate + 'X';
            break;
          default:
            map.contract_num = 0;
            map.contract_show = T('双向') + pair.leverRate + 'X';
            break;
        }
      }
      return list;
    },

    /*************************************************通信触发*/
    //完全显示
    viewappear() {
      if (onShow) {
        this.httpRanking();
      }
      this.onShow = true;
    },
    
    /********************************* HTTP*/
    //http榜单-刷新
    httpRanking(load) {

      if (this.isRefresh) return;
      load ? jmodal.showLoad() : (this.isRefresh = true);

      var param = {'isContract':this.menuKeys[this.menu]};
      jhttps.post("billboard/queryExecuteRankList",param,true,(data) => {
        
        if (load) jmodal.hideLoad();
        this.isRefresh = false;
        if (data.status == 200) {
          data = data.data;
          this.datas = this.initData(data.executeRankList);
        } else {
          this.datas = [];
          jmodal.toast(data.msg);
        }
      });
    },

    //http用户信息
    httpUserInfo(item) {
      jmodal.showLoad();
      var param = {};
      jhttps.postX("userV2/userDetail", param, true,(data) => {
        if (data.status == 200) {
          data = data.data;
          var info = data.account;
          if (info.member != 'LV0') {
            this.httpGetCopyStrategyParamInfo(item);
          } else {
             var parm = {message:T('开通VIP后才能使用参数复制功能，是否前往开启？'),
                        okBtn:T("前往"), cancelBtn:T("取消")};
            jmodal.confirm(parm, (bool)=>{
              if(bool == "true") {
                jnavigator.open('', "mine/Vip", {}, {'navColor':'#1677fe'});
              }
            });
          }
        } else {
          jmodal.hideLoad();
          jmodal.toast(data.msg);
        }
      });
    },
    

    //http查询复制信息（价格、是否支付过）
    httpGetCopyStrategyParamInfo(item) {
      jmodal.showLoad();
      var param = {'executeId': item.executeId};
      jhttps.post("execute/getCopyStrategyParamInfo", param, true,(data) => {
        jmodal.hideLoad();
        if (data.status == 200) {
          
          data = data.data;
          if (data.isNeedVtn) {
            jmodal.hideLoad();
            var parm = {message:T("复制该策略参数需要支付")+data.copyNeedVtn+'VTN(含提现权益)。',
                        okBtn:T("支付"), cancelBtn:T("取消")};
            jmodal.confirm(parm, (bool)=>{
              if(bool == "true") {
                this.httpGetCopyStrategyParam(item);
              }
            });
          } else {
            this.copyUp(item);
          }

        } else {
          jmodal.toast(data.msg);
        }
      });
    },

    //http复制参数
    httpGetCopyStrategyParam(item) {
      var param = {'executeId': item.executeId};
      jhttps.postX("execute/copyStrategyParam", param, true,(data) => {
        jmodal.hideLoad();
        if (data.status == 200) {
          this.copyUp(item);
        } else {
          jmodal.toast(data.msg);
        }
      });
    },
    
    /********************************** 点击触发*/
    //菜单点击
    menuClick(menu){
      if (this.isRefresh) return;
      this.menu = menu;
      this.httpRanking(true);
    },
    
    //策略详情
    infoClick(item) {
      if (!jconfig.forceLogin()) return;

      var param = {'executeId':item.executeId};
      jnavigator.open('', "follw/UFollwStrategyInfo",param);
    },

    //跟单
    followClick(item) {
      if (!jconfig.forceLogin()) {
        return;
      }
      
      var param = {
        'executeId':item.executeId,
        'strategyId':item.strategyId,
        'investCoin':item.investCoin,
        'followName':item.followName,
        'commission':item.commission,
        'isVtnEquity':item.isVtnEquity,
        'exchangeName':item.exchangeName,
        'counterCoin':item.counterCoin,
        'baseCoin':item.baseCoin,
        'minInvest':item.investLimit.minInvest,
        'maxInvest':item.investLimit.maxInvest};
      if (item.isContract == 'spot') {
        jnavigator.open('', "follw/OnFollw",param);
      } else {
        param.contractId = item.contractId;
        jnavigator.open('', "follw/OnFollwContract",param);
      }
    },

    //复制启动点击
    copyClick(item) {
      if (!jconfig.forceLogin()) return;
      
      this.httpUserInfo(item);
    },

    //复制启动
    copyUp(item) {

      var copy = {'strategyId':item.strategyId, 'strategyName':item.strategyName, 'copyConfig':item.config};
      var dict = {'网格交易': 'up/SUpGrid',
                    '海浪网格': 'up/SUpSea',
                    '马丁追踪': 'up/SUpMd',
                    '区间套利': 'up/SUpQj',
                    '恒量定投': 'up/SUpWeigh',
                    '海风马丁': 'up/SUpHf',
                    '币本位海风马丁': 'up/SUpHfTb',
                    'DCA智能策略': 'up/SUpTy'};
      var url = item.isContract == 'spot' ?  dict[copy.strategyName] : 'upContract/CUp';
      if (!url){
        jmodal.toast('无法复制非官方策略');
        return;
      }
      jnavigator.open(copy.strategyName, url, copy);
    }
  },
};
</script>
